package com.mapull.date;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Period;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;

/**
 * @author mapull
 */
public class MapullDateCalc {

    public static void main(String[] args) {
//        calendarCalc();
//        localDateCalc();
        jodaTimeCalc();
    }

    private static void calendarCalc(){
        Calendar calendar = Calendar.getInstance();
        System.out.println("初始时间 " + calendar.getTime());

        calendar.add(Calendar.DATE, 3);
        System.out.println("加3天后 " + calendar.getTime());

        calendar.add(Calendar.DATE, -3);
        System.out.println("减3天后 " + calendar.getTime());

        calendar.add(Calendar.MONTH, 2);
        System.out.println("加2个月 " + calendar.getTime());
    }

    private static void localDateCalc(){
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime minus5Days = dateTime.minus(5, ChronoUnit.DAYS);


        LocalDateTime minus2Days = dateTime.minusDays(2);

        LocalDateTime plus2Hours = dateTime.plusHours(2);

        LocalDateTime plusMonth = dateTime.plus(1, ChronoUnit.MONTHS);

        System.out.println("初始时间 " + dateTime);
        System.out.println("  5天前 " + minus5Days);
        System.out.println("  2天前 " + minus2Days);
        System.out.println("加2小时 " + plus2Hours);
        System.out.println("1个月后 " + plusMonth);
        System.out.println("初始时间 " + dateTime);
    }

    private static void jodaTimeCalc(){
        DateTime dateTime = new DateTime(2022,4,3,22,25,13);

        DateTime minus5Days = dateTime.minus(Period.days(5));
        DateTime minus2Days = dateTime.minusDays(2);
        DateTime plus2Hours = dateTime.plus(Duration.standardHours(2));
        DateTime plusMonth = dateTime.plusMonths(1);

        System.out.println("初始时间 " + dateTime);
        System.out.println("  5天前 " + minus5Days);
        System.out.println("  2天前 " + minus2Days);
        System.out.println("加2小时 " + plus2Hours);
        System.out.println("1个月后 " + plusMonth);
        System.out.println("初始时间 " + dateTime);
    }
}
